from rest_framework import viewsets

from customauth.models import MyUser
from naryad.models import It_lab, Pl_uslp, User_sdl
from .serializers import It_labSerializer, Pl_uslpSerializer, UserSerializer

# class It_labTwhoViewSet(viewsets.ModelViewSet):
#     queryset =

ST_TECH: int = 3500


class UserViewSet(viewsets.ModelViewSet):
    """API endpoint that allows users to be viewed or edited."""

    queryset = MyUser.objects.all()
    serializer_class = UserSerializer


class Pl_uslpViewSet(viewsets.ModelViewSet):  # noqa: N801
    queryset = Pl_uslp.objects.none()
    serializer_class = Pl_uslpSerializer

    def get_queryset(self):  # noqa: ANN201
        idkv = self.request.query_params.get("idkv")
        return Pl_uslp.objects.filter(idkv=idkv).all()


class It_labViewSet(viewsets.ModelViewSet):  # noqa: N801
    queryset = It_lab.objects.none()
    serializer_class = It_labSerializer

    def get_queryset(self):  # noqa: ANN201, PLR0912, C901
        mpp = self.request.query_params.get("mpp")
        idkv = self.request.query_params.get("idkv")
        waiting = self.request.query_params.get("waiting")
        tech_st = self.request.query_params.get("tech_st")
        id_nar = self.request.query_params.get("id")
        lpu = self.request.query_params.get("lpu")
        all_nar = self.request.query_params.get("all")
        if idkv:
            return (
                It_lab.objects.filter(idkv=idkv, dz_etap__isnull=False)
                .select_related(
                    "lpu",
                    "patient",
                    "opl",
                    "sdl_etap",
                    "sdl_set",
                    "naryad_doc",
                    "user_etap_link",
                )
                .order_by("id_etap")
                .all()
            )
        if id_nar:
            return (
                It_lab.objects.filter(id=id_nar)
                .select_related(
                    "lpu",
                    "patient",
                    "opl",
                    "sdl_etap",
                    "sdl_set",
                    "naryad_doc",
                    "user_etap_link",
                )
                .all()
            )
        if mpp:
            if waiting:
                user = MyUser.objects.filter(mpp=mpp).first()
                sdls = User_sdl.objects.filter(user=user).first()
                query_sdls = sdls.sdls.all()
                list_sdls = list(query_sdls.values_list("sdl", flat=True))
                if ST_TECH in list_sdls:
                    queryset = (
                        It_lab.objects.filter(
                            mpp_etap=mpp,
                            dz_etap__isnull=False,
                            id_etap__gt=1,
                            dzr__isnull=True,
                            pdzr__isnull=True,
                            check_zav=False,
                            check_tech=False,
                        )
                        .order_by("idkv", "-id_etap")
                        .distinct("idkv")
                        .select_related(
                            "lpu",
                            "patient",
                            "opl",
                            "sdl_etap",
                            "sdl_set",
                            "naryad_doc",
                            "user_etap_link",
                        )
                        .all()
                    )
                else:
                    queryset = (
                        It_lab.objects.filter(
                            mpp_etap=mpp,
                            dz_etap__isnull=False,
                            dzr__isnull=True,
                            pdzr__isnull=True,
                        )
                        .order_by("idkv", "-id_etap")
                        .distinct("idkv")
                        .select_related(
                            "lpu",
                            "patient",
                            "opl",
                            "sdl_etap",
                            "sdl_set",
                            "naryad_doc",
                            "user_etap_link",
                        )
                        .all()
                    )
                qs_all = It_lab.objects.none()
                for one_query in queryset:
                    number = one_query.id_last.number
                    qs_all |= (
                        It_lab.objects.exclude(mpp_etap=mpp)
                        .select_related(
                            "lpu",
                            "patient",
                            "opl",
                            "sdl_etap",
                            "sdl_set",
                            "naryad_doc",
                            "user_etap_link",
                        )
                        .filter(id=number, dzr__isnull=True, pdzr__isnull=True)
                        .all()
                    )
                for one in qs_all:
                    if one.user_etap_link is None:
                        one.user_etap_link = MyUser.objects.get(login="none")
                return qs_all
            if tech_st:
                if lpu is not False and lpu != "false" and lpu is not None:
                    if all_nar is True or all_nar == "true":
                        queryset = (
                            It_lab.objects.filter(
                                id_etap__gt=1,
                                dz_etap__isnull=True,
                                lpu__lpu=lpu,
                            )
                            .select_related(
                                "lpu",
                                "patient",
                                "opl",
                                "sdl_etap",
                                "sdl_set",
                                "naryad_doc",
                                "user_etap_link",
                            )
                            .all()
                        )
                    else:
                        queryset = (
                            It_lab.objects.filter(
                                id_etap=1,
                                dz_etap__isnull=True,
                                lpu__lpu=lpu,
                            )
                            .select_related(
                                "lpu",
                                "patient",
                                "opl",
                                "sdl_etap",
                                "sdl_set",
                                "naryad_doc",
                                "user_etap_link",
                            )
                            .all()
                        )
                elif all_nar == "true" or all_nar is True:
                    queryset = (
                        It_lab.objects.filter(id_etap__gt=1, dz_etap__isnull=True)
                        .select_related(
                            "lpu",
                            "patient",
                            "opl",
                            "sdl_etap",
                            "sdl_set",
                            "naryad_doc",
                            "user_etap_link",
                        )
                        .all()
                    )
                else:
                    queryset = (
                        It_lab.objects.filter(id_etap=1, dz_etap__isnull=True)
                        .select_related(
                            "lpu",
                            "patient",
                            "opl",
                            "sdl_etap",
                            "sdl_set",
                            "naryad_doc",
                            "user_etap_link",
                        )
                        .all()
                    )
                return queryset
            return (
                It_lab.objects.filter(dzr__isnull=True, pdzr__isnull=True, dz_etap__isnull=True, mpp_etap=mpp)
                .select_related(
                    "lpu",
                    "patient",
                    "opl",
                    "sdl_etap",
                    "sdl_set",
                    "naryad_doc",
                    "user_etap_link",
                )
                .prefetch_related("images")
                .all()
            )

        return queryset
